package com.vinjogames.ui;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.entities.Game;
import com.vinjogames.gfx.Image;
import com.vinjogames.util.GameConVars;
import com.vinjogames.util.InputManager;
import com.vinjogames.util.Preferences;

public class InGameMenu {
	float mGLWidth;
	float mGLHeight;
	PointF mBotLeft;
	PointF mTopRight;
	PointF mBackgroundLocation;
	Image mBackground;
	Game mGame;
	
	PressableButton mExitButton;
	PressableButton mBackButton;
	
	PressableButton mDebugButton;
	
	public InGameMenu(Game game, float width, float height, PointF bottomLeft, PointF topRight){
		mGame     = game;
		mGLWidth  = width;
		mGLHeight = height;
		mBotLeft  = bottomLeft;
		mTopRight = topRight;
	}
	
	public void load(){
		mBackground = new Image(GameConVars.MENUBACKGROUND,mGLWidth, mGLHeight);
		mBackgroundLocation = new PointF(mBotLeft.x + mGLWidth/2,mBotLeft.y + mGLHeight /2);
		PointF bl = new PointF(mBotLeft.x + 1, mTopRight.y - 2.5f);
		PointF tr = new PointF(mTopRight.x -1, mTopRight.y - 1.5f);
		
		mDebugButton = new PressableButton("Debug: " + Preferences.getDebugSetting(), new PointF(bl.x, bl.y),new PointF(tr.x, tr.y));
		
		bl.set(bl.x, bl.y - 1);
		tr.set(tr.x, tr.y - 1);
		
		mExitButton = new PressableButton("Exit", new PointF(bl.x, bl.y),new PointF(tr.x, tr.y));
		
		bl.set(bl.x, bl.y - 1);
		tr.set(tr.x, tr.y - 1);
		
		mBackButton = new PressableButton("Back", new PointF(bl.x, bl.y),new PointF(tr.x, tr.y));
	}
	
	public void draw(GL10 gl){
		mBackground.draw(gl, mBackgroundLocation);
		
		mDebugButton.draw(gl);
		mBackButton.draw(gl);
		mExitButton.draw(gl);
	}

	public boolean insideBox(PointF p){
		return p.x >= mBotLeft.x && p.y >= mBotLeft.y & p.x <= mTopRight.x && p.y <= mTopRight.y;
	}
	
	public void handleAction(int action, PointF glLocation){
		if(action == GameConVars.INPUT_TAP){
			if(mDebugButton.insideBox(glLocation)){
				Preferences.ToggleDebug();
				mDebugButton.setText("Debug: " + Preferences.getDebugSetting());
			}
			if(mBackButton.insideBox(glLocation)){
				mGame.onMenuButton();
			}
			if(mExitButton.insideBox(glLocation)){
				InputManager.getInstance().quit();
			}
		}
	}
}
